
package com.admissions;

/*
 * Name: $RCSfile: BaseActivity.java,v $
 * Version: $Revision: 1.1 $
 * Date: $Date: Dec 15, 2011 1:56:00 PM $
 *
 * Copyright (C) 2011 DirecTV, Inc. All rights reserved.
 */

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.admissions.common.Constants;

/**
 * This class is base class for total activity of project. every class extend
 * from it,must have full method,filed of it.
 */
public class BaseActivity extends Activity implements OnClickListener, Constants {
    private ProgressDialog mProgressDialog;

    /**
     * variable to cache image
     */
    public static HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();

    /**
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindowParameters();
        mProgressDialog = new ProgressDialog(getDialogContext());
    }

    /**
     * setup to windown hide title bar, the screen is more full.
     */

    private void initWindowParameters() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    }

    /**
     * every event about click of component will process in here.
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    /**
     * check for network's status if return o==>no connection network. if !=0
     * ==> have connection network.
     * 
     * @return
     */
    public int getStatusNetWorking() {
        final ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifi.isAvailable() && wifi.isConnected()) {
            return 1;
        } else if (mobile.isAvailable() && mobile.isConnected()) {
            return 2;
        } else {
            return 0;
        }
    }

    /**
     * this method used to display info and close activity
     */
    public void closeApp() {
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setTitle(R.string.title_dialog);
        alt_bld.setIcon(R.drawable.icon);
        alt_bld.setMessage(this.getResources().getString(R.string.close_app_mesagess))
                .setCancelable(false)
                .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                        startActivity(getExitIntentApplication());
                        finish();
                    }
                }).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // Action for 'NO' Button
                        dialog.cancel();
                    }
                });

        alt_bld.show();
    }

    /**
     * this method used to display a dialog.
     * 
     * @param isFinishedActivity
     *            a flag
     */
    public void showDialog(String title, String message, final boolean isFinishedActivity) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(title);
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setMessage(message);
        alertDialog.setButton(getResources().getString(R.string.btn_ok),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (isFinishedActivity) {
                            finish();
                        }
                    }
                });
        alertDialog.show();
    }

    /**
     * this method used to display a dialog with resource id input.
     * 
     * @param isFinishedActivity
     *            a flag
     */
    public void showDialog(int titleId, int messageId, final boolean isFinishedActivity) {
        this.showDialog(getResources().getString(titleId), getResources().getString(messageId),
                isFinishedActivity);
    }

    /**
     * show dialog with invalid connection.
     */
    public void showDialogWithInvalidConnection() {
        this.showDialog(this.getResources().getString(R.string.title_dialog), this.getResources()
                .getString(R.string.err_invalid_network), false);
    }

    public void showNetworkMesagess() {
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setTitle(R.string.title_dialog);
        alt_bld.setIcon(R.drawable.icon);
        alt_bld.setMessage(this.getResources().getString(R.string.err_invalid_network))
                .setCancelable(false)
                .setPositiveButton(R.string.btn_setting, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                    }
                }).setNegativeButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // Action for 'NO' Button
                        dialog.cancel();
                    }
                });

        alt_bld.show();
    }

    public void showDialogWithQuestions(int positiveButtonLable, int negativeButton, int mesagess,
            final Intent intent) {
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setTitle(R.string.title_dialog);
        alt_bld.setIcon(R.drawable.icon);
        alt_bld.setMessage(this.getResources().getString(mesagess)).setCancelable(false)
                .setPositiveButton(positiveButtonLable, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startActivity(intent);
                    }
                }).setNegativeButton(negativeButton, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // Action for 'NO' Button
                        dialog.cancel();
                    }
                });

        alt_bld.show();
    }

    /**
     * Get activity for exiting app.
     * 
     * @return intent with flag.
     */
    public Intent getExitIntentApplication() {
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return startMain;
    }

    public Context getDialogContext() {
        Context context;
        if (getParent() != null)
            context = getParent();
        else
            context = this;
        return context;
    }

    public void showLoadingDialog(String msg) {
        ProgressDialog pd = mProgressDialog;
        if (msg != null) {
            pd.setMessage(msg);
        }
        pd.setCancelable(true);
        pd.show();
    }

    public void showLoadingDialog() {
        showLoadingDialog("Loading...");
    }

    public void dismissLoadingDialog() {
        mProgressDialog.dismiss();
    }
}
